package com.example;

import com.sun.btrace.annotations.*;
import com.sun.btrace.*;
import static com.sun.btrace.BTraceUtils.*;

@BTrace
public class BesHttpSessionTrace {
	private static int m = 0, n = 0, p = 0;
	private static boolean stracktrace_verbose = false;

	@OnMethod(
			clazz = "com.bes.enterprise.webtier.session.ManagerBase",
			method = "createSession",
			location = @Location(value = Kind.RETURN))
	public static void OnHttpSessionCreate(@Return Object ret) {
		println(strcat(strcat(timestamp("yyyy-MM-dd HH:mm:ss"), " -> "), "Session has been created."));
		println("---------------------------------------------------------------------");
		println(strcat("Session Id : ", str(get(field(classOf(ret), "id"), ret))));
		println(strcat("Session Object Name : ", str(ret)));
		if (stracktrace_verbose) {
			println("Stack Trace :");
			jstack();
		}
		println();
		m++;
	}
	
	@OnMethod(
			clazz = "com.bes.enterprise.webtier.session.DefaultSession", 
			method = "setAttribute",
			type = "void (java.lang.String, java.lang.Object, boolean)"
	)
	public static void OnSetAttribute(@Self Object self, AnyType[] args) {
		println(strcat(strcat(timestamp("yyyy-MM-dd HH:mm:ss"), " -> "), "Session has been changed for setting attribute."));
		println("---------------------------------------------------------------------");
		println(strcat("Session Id : ", str(get(field(classOf(self), "id"), self))));
		println(strcat("Session Object Name : ", str(self)));
		println(strcat("Set Attribute Name : ", str(args[0])));
		println(strcat("Set Attribute Value : ", str(args[1])));
		if (stracktrace_verbose) {
			println("Stack Trace :");
			jstack();
		}
		println();
	}
	
	@OnMethod(
			clazz = "com.bes.enterprise.webtier.session.DefaultSession", 
			method = "removeAttribute",
			type = "void (java.lang.String, boolean)"
			)
	public static void OnRemoveAttribute(@Self Object self, AnyType[] args) {
		println(strcat(strcat(timestamp("yyyy-MM-dd HH:mm:ss"), " -> "), "Session has been changed for removing attribute."));
		println("---------------------------------------------------------------------");
		println(strcat("Session Id : ", str(get(field(classOf(self), "id"), self))));
		println(strcat("Session Object Name : ", str(self)));
		println(strcat("Remove Attribute Name : ", str(args[0])));
		if (stracktrace_verbose) {
			println("Stack Trace :");
			jstack();
		}
		println();
	}
	
	@OnMethod(
			clazz = "com.bes.enterprise.webtier.session.DefaultSession", 
			method = "invalidate"
			)
	public static void OnHttpSessionInvalidate(@Self Object self) {
		println(strcat(strcat(timestamp("yyyy-MM-dd HH:mm:ss"), " -> "), "Session has been invalidated."));
		println("---------------------------------------------------------------------");
		println(strcat("Session Id : ", str(get(field(classOf(self), "id"), self))));
		println(strcat("Session Object Name : ", str(self)));
		if (stracktrace_verbose) {
			println("Stack Trace :");
			jstack();
		}
		println();
		n++;
	}
	
	@OnMethod(
			clazz = "com.bes.enterprise.webtier.session.DefaultSession",
			method ="isValid",
			location = @Location(value = Kind.CALL, clazz = "/.*/", method = "expire")
			)
	public static void OnCleanupExpiredSessions(@Self Object self, AnyType[] args) {
		println(strcat(strcat(timestamp("yyyy-MM-dd HH:mm:ss"), " -> "), "Cleanup expired session."));
		println("---------------------------------------------------------------------");
		println(strcat("Session Id : ", str(get(field(classOf(self), "id"), self))));
		println(strcat("Session Object Name : ", str(self)));
		if (stracktrace_verbose) {
			println("Stack Trace :");
			jstack();
		}
		println();
		p++;
	}
	
	@OnTimer(300000)
	public static void count() {
		if (m!= 0) {
			println(strcat(strcat(timestamp("yyyy-MM-dd HH:mm:ss"), " -> "), "Total session statistical result :"));
			println("---------------------------------------------------------------------");
			println(strcat("Total session create number: ", str(m)));
			println(strcat("Total session invalidated number: ", str(n)));
			println(strcat("Total session cleared for timeout number: ", str(p)));
			println();
		}
	}
}